package com.styytech.dadabao.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.MyAccountResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseFragment;
import com.styytech.dadabao.bean.UserAccount;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.ui.main.MainActivity;
import com.styytech.dadabao.ui.my.accountset.AccountSetActivity;
import com.styytech.dadabao.ui.my.accountset.AuthNameActivity;
import com.styytech.dadabao.ui.my.accountset.BankCardManageActivity;
import com.styytech.dadabao.ui.my.accountset.PayforSetActivity;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.SpUser;
import com.styytech.dadabao.widge.dialog.EnsureDialog;
import com.styytech.dadabao.widge.dialog.IsInNumDialog;
import com.styytech.dadabao.widge.dialog.MoneyDialog;

public class MyFragment extends BaseFragment {
    private View rootView;


    @ViewInject(R.id.ll_exist_cash)//充值
    private LinearLayout ll_exist_cash;
    @ViewInject(R.id.ll_available_balance)//提现
    private LinearLayout ll_available_balance;

    @ViewInject(R.id.tv_exist_cash)//现有资金
    private TextView tv_exist_cash;
    @ViewInject(R.id.tv_available_balance)//可用余额
    private TextView tv_available_balance;
    @ViewInject(R.id.tv_earn)//赚取
    private TextView tv_earn;

    @ViewInject(R.id.tv_recharge)//充值
    private TextView tv_recharge;
    @ViewInject(R.id.tv_get_cash)//提现
    private TextView tv_get_cash;


    @ViewInject(R.id.tv_friends)//邀请好友
    private TextView tv_friends;
    @ViewInject(R.id.tv_account_set)//账号设置
    private TextView tv_account_set;
    @ViewInject(R.id.tv_notice)//站内通知
    private TextView tv_notice;
    @ViewInject(R.id.tv_user_Record)//投资记录
    private TextView tv_user_Record;
    @ViewInject(R.id.tv_trading_detial)//交易明细
    private TextView tv_trading_detial;

    IsInNumDialog isInNumDialog;
    private boolean isFrist = true;
    EnsureDialog cardDialog;
    EnsureDialog bankDialog;
    MoneyDialog moneyDialog;
    private Double hava_money;
    private Double availableBalance= (double)-1;
    EnsureDialog passwordDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = super.onCreateView(inflater, container, savedInstanceState,
                R.layout.fragment_my);
        return rootView;
    }


    public void onResume() {
        getMyAccount();
        super.onResume();
    }

    /**
     * 重新加载
     */
    protected void reloadBtnOnclick() {
        isFrist = true;
        getMyAccount();
    }

    /**
     * 网络连接正常
     */
    protected void connectTivityService() {
        getMyAccount();
    }

    /**
     * 点击事件
     */
    @OnClick({R.id.tv_recharge, R.id.tv_get_cash, R.id.tv_friends, R.id.tv_account_set, R.id.tv_notice, R.id.tv_user_Record, R.id.tv_trading_detial})
    public void onFragmentClick(View view) {
        switch (view.getId()) {
            case R.id.tv_recharge:
                if (SpUser.getIdCardVStatus(getmActivity()) == 0) {
                    //判断是否实名认证
                    showCardDialog();
                } else {
                    startActivity(new Intent(getmActivity(), RechargeActivity.class));
                }
                break;
            case R.id.tv_get_cash:

                if (SpUser.getIdCardVStatus(getmActivity()) == 0) {
                    //判断是否实名认证
                    showCardDialog();
                } else {
                   // showbankOrZfbDialog();
                    //判断是否绑定过银行卡
                    if (SpUser.getBankCardVStatus(getmActivity()) == 0) {
                        showBankCardDialog();
                    }else if(SpUser.getBuyPasswordStatus(getmActivity())==0){
                        //判断是否设置支付密码
                        showPassWordDialog();
                    } else if (availableBalance < 0) {
                        CommonUtils.showToastMsg(getmActivity(), "获取可用余额信息失败，请稍后再试");
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(getmActivity(), WithdrawalsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putDouble("now_money", availableBalance);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
                break;
            case R.id.tv_friends:
                CommonUtils.startActivity(getmActivity(), InviteFriendsActivity.class);
                break;
            case R.id.tv_account_set:// 账户设置
                CommonUtils.startActivity(getmActivity(), AccountSetActivity.class);
                break;
            case R.id.tv_notice:// 站内通知
                CommonUtils.startActivity(getmActivity(), MessageFragmentActivity.class);
                break;
            case R.id.tv_user_Record:
                //投资记录
                startActivity(new Intent(getmActivity(), UserRecordActivity.class));
                break;
            case R.id.tv_trading_detial:
                //交易明细
                startActivity(new Intent(getmActivity(), TransactionDetailsActivity.class));
                break;
        }
    }

    //需要完成实名认证
    private void showCardDialog() {
        cardDialog = new EnsureDialog(getmActivity(), "提示", "您尚未完成实名认证，是否立即完善？", 1, "确定");
        cardDialog.setEnsureDialogCallback(ensureCardcallback);
        cardDialog.setCanceleDialogCallback(cancelCardgcallback);
        cardDialog.NotCancel(false);
        cardDialog.show();
    }
    private void showbankOrZfbDialog(){
        moneyDialog=new MoneyDialog(getmActivity());
        moneyDialog.setToBankcallback(toBankcallback);
        moneyDialog.setToZfbcallback(toZfbcallback);
        moneyDialog.NotCancel(false);
        moneyDialog.show();
    }
    //需要完成绑定银行卡
    private void showBankCardDialog() {
        bankDialog = new EnsureDialog(getmActivity(), "提示", "您尚未绑定提现银行卡，是否立即绑定？", 1, "确定");
        bankDialog.setEnsureDialogCallback(ensureBankCardcallback);
        bankDialog.setCanceleDialogCallback(cancelBankcallback);
        bankDialog.NotCancel(false);
        bankDialog.show();
    }
    //您尚未设置支付密码，是否立即设置？
    private void showPassWordDialog(){
        passwordDialog = new EnsureDialog(getmActivity(), "提示", "您尚未设置支付密码，是否立即设置？", 1,"确定");
        passwordDialog.setEnsureDialogCallback(ensurePassDialogcallback);
        passwordDialog.setCanceleDialogCallback(cancelPassDialogcallback);
        //如果强制升级(点其他位置或back不能取消)
        passwordDialog.NotCancel(false);
        passwordDialog.show();
    }
    /**
     * 体现到支付宝*/
    MoneyDialog.Dialogcallback toZfbcallback=new MoneyDialog.Dialogcallback(){
        @Override
        public void dialogdo() {
            if(SpUser.getBuyPasswordStatus(getmActivity())==0){
                //判断是否设置支付密码
                showPassWordDialog();
            } else if (availableBalance < 0) {
                CommonUtils.showToastMsg(getmActivity(), "获取可用余额信息失败，请稍后再试");
            } else {
                Intent intent = new Intent();
                intent.setClass(getmActivity(), WithdrawalsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putDouble("now_money", availableBalance);
                intent.putExtras(bundle);
                startActivity(intent);
            }
            moneyDialog.dismiss();
        }

    };
    /**
     * 体现到银行卡*/
    MoneyDialog.Dialogcallback toBankcallback=new MoneyDialog.Dialogcallback(){
        @Override
        public void dialogdo() {
            //判断是否绑定过银行卡
            if (SpUser.getBankCardVStatus(getmActivity()) == 0) {
                showBankCardDialog();
            }else if(SpUser.getBuyPasswordStatus(getmActivity())==0){
                //判断是否设置支付密码
                showPassWordDialog();
            } else if (availableBalance < 0) {
                CommonUtils.showToastMsg(getmActivity(), "获取可用余额信息失败，请稍后再试");
            } else {
                Intent intent = new Intent();
                intent.setClass(getmActivity(), WithdrawalsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putDouble("now_money", availableBalance);
                intent.putExtras(bundle);
                startActivity(intent);
            }
            moneyDialog.dismiss();
        }

    };

    /**
     * 设置实名认证dialog需要处理的事情
     */
    EnsureDialog.Dialogcallback ensureCardcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            showIsInDialog();
            cardDialog.dismiss();

        }
    };
    EnsureDialog.Dialogcallback cancelCardgcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            cardDialog.dismiss();

        }
    };

    private void showIsInDialog(){
        isInNumDialog=new IsInNumDialog(getmActivity());
        isInNumDialog.setInCallBack(incallback);
        isInNumDialog.setOutCallback(outcallback);
        isInNumDialog.NotCancel(false);
        isInNumDialog.show();

    }
    IsInNumDialog.Dialogcallback incallback = new IsInNumDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            Intent intent = new Intent();
            intent.setClass(getmActivity(), AuthNameActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("type", 1);
            intent.putExtras(bundle);
            startActivity(intent);
            isInNumDialog.dismiss();

        }
    };
    IsInNumDialog.Dialogcallback outcallback = new IsInNumDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            Intent intent = new Intent();
            intent.setClass(getmActivity(), AuthNameActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("type", 2);
            intent.putExtras(bundle);
            startActivity(intent);
            isInNumDialog.dismiss();

        }
    };
    /**
     * 设置绑定银行卡需要处理的事情
     */
    EnsureDialog.Dialogcallback ensureBankCardcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            startActivity(new Intent(getmActivity(), BankCardManageActivity.class));
            bankDialog.dismiss();

        }
    };
    EnsureDialog.Dialogcallback cancelBankcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            bankDialog.dismiss();

        }
    };
    /**
     * 设置支付密码Dialog需要处理的事情
     */
    EnsureDialog.Dialogcallback ensurePassDialogcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {

            startActivity(new Intent(getmActivity(), PayforSetActivity.class));
            passwordDialog.dismiss();

        }
    };
    EnsureDialog.Dialogcallback cancelPassDialogcallback = new EnsureDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            passwordDialog.dismiss();
        }
    };

/***************************** 我的账户 ***************************************/
    /**
     * 获取我账户信息
     */
    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            UserAccount userAccount = (UserAccount) object;
            if (userAccount == null) {
                setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "账号信息获取失败");
            } else {
                setView(View.GONE, View.VISIBLE, View.VISIBLE, View.GONE, "获取账号信息...");
                ll_exist_cash.setVisibility(View.VISIBLE);
                ll_available_balance.setVisibility(View.VISIBLE);
            }
            availableBalance = userAccount.getAvailableBalance();
            tv_exist_cash.setText(userAccount.getAccountbalanceF() + "");
            tv_available_balance.setText(userAccount.getAvailableBalanceF() + "");
            tv_earn.setText(userAccount.getAssignedIncomeAmount() + "");
        }

        @Override
        public void executeError(String message, int errorCode) {
            if (AbstractResponseResult.SERVER_FAILURE_LOGIN_FAILURE == errorCode) {
                Intent intent = new Intent(getmActivity(), MainActivity.class);
                getmActivity().startActivity(intent);
            } else {
                setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "账号信息获取失败");
            }
        }

        @Override
        public void HttpError(String message, int errorCode) {
            setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "账号信息获取失败");
        }
    };

    public void getMyAccount() {

        if (isFrist) {
            setView(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE, "获取账号信息...");
            ll_exist_cash.setVisibility(View.GONE);
            ll_available_balance.setVisibility(View.GONE);

            isFrist = !isFrist;
        }

        String url = ConstantsServerUrl.getUserAccountUrl();
        RequestParams params = new RequestParams();

        MyAccountResult logResult = new MyAccountResult(responseResult);
        try {
            new HttpRequest(context).doPost(logResult, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }

}
